<?php
namespace app\index\controller;

use think\App;
use think\Controller;
use think\facade\Cookie;
use think\facade\Request;
use think\facade\Url;

/**
 * Created by liuyang.
 * Class Sync
 * @package app\index\controller
 */
class Sync extends Controller
{

    private $regUrl;
    private $loginUrl;
    private  $loginUser;
    private  $loginPwd;

    public function initialize()
    {
        Url::root('/index.php');
        $this->loginUser = "admin@rykj.com";
        $this->loginPwd = "888888";
        $this->regUrl = "http://rykj.gensee.com/integration/site/webcast/user/created";
        $this->loginUrl = "http://rykj.gensee.com/training/site/login";
        parent::initialize(); // TODO: Change the autogenerated stub
    }


    /**
     * Created by liuyang.
     * 注册用户
     */
    public function register()
    {
        if(Request::isAjax()) {
            $data = Request::post();
            $name = $data['name'];
            $userLoginName = $data['userLoginName'];
            $userPassword = $data['userPassword'];

            $params = "loginName=$this->loginUser&password=$this->loginPwd&sec=&name=$name&state=".$data['state']."&address=".$data['address']."&company=".$data['company'].
                "&country=".$data['country']."&description=".$data['description']."&firstName=".$data['firstName']."&job=".$data['job']."&lastName=".$data['lastName'].
                "&male=".$data['male']."&mobile=".$data['mobile']."&nickName=".$data['nickName']."&phone=".$data['phone']."&role=&zipcode=".$data['zipcode']."&maxPoints=&effectiveDate=&invalidDate=&userLoginName=$userLoginName&userPassword=$userPassword";
            $ch   = curl_init();
            curl_setopt($ch, CURLOPT_URL, $this->regUrl);
            curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
            curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
            $result = curl_exec($ch);
            curl_close($ch);
            $result = json_decode($result,true);
            if($result['code'] == 0){
                //只要是注册成功，都删除原有的用户并且新增
                db('users')->where(['userLoginName'=>$userLoginName])->delete();
                $data['userPassword'] = bhmd5($data['userPassword']);
                db('users')->insert($data);
            }
            $this->success($result);

        }else{
            $this->error('非法操作');
        }
    }
    /**
     * 登录
     * Created by liuyang.
     * @return \think\response\Json
     */
    public function login(){
        if (Request::isAjax()) {
            $data = Request::post();
            $user = $data['userLoginName'];
            $pass = $data['userPassword'];
            if ( $user == '' || $pass == '' ) return json(['state'=>0,'msg'=>'请输入账号和密码！']);
            $user = db('users')->field('id,usertype,userLoginName,userPassword,name')->where(['userLoginName'=>$user])->find();
            if ( !$user ) return json(['state'=>0,'msg'=>'登录错误，请重试！']);
            if ($data['userPassword'] != bhmd5($user['userPassword'],true)) return json(['state'=>0,'msg'=>'密码错误，请重试！']);
            if($user['usertype'] == 1){
                Cookie::set('userid',$user['id'],['expire'=>3600]);
                Cookie::set('username',$user['userLoginName'],['expire'=>3600]);
                Cookie::set('name',$user['name'],['expire'=>3600]);
                return json(['state'=>1,'msg'=>'登录成功！','act'=>'s']);
                //跳转学生登录成功页面
            }else{
                return json(['state'=>1,'msg'=>'登录成功！','act'=>'t']);
            }
        }
    }


    public function testWebCast(){

        $url = "http://rykj.gensee.com/training/site/s/25538270";
        $params = "visible=true&gsVer=&nickname=test&token=75060";
        $ch   = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
        curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
//        curl_setopt($ch, CURLOPT_HEADER, 1);
        curl_setopt($ch, CURLINFO_HEADER_OUT, true);
        $result = curl_exec($ch);



        // 获得响应结果里的：头大小
//        $headerSize = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
        // 根据头大小去获取头信息内容
//        $header = substr($result, 0, $headerSize);

        //没找到页面
        preg_match_all('/<title>(.*)<\/title>/i',$result,$content);
        if(strpos($content[1][0], '找不到您需要的页面') !== false){
            return json(['state'=>0,'msg'=>'直播页面不存在，请确认直播ID是否输入正确']);
        }



    }


}
